﻿/*
 * Сделано в SharpDevelop.
 * Пользователь: Eugenij
 * Дата: 29.01.2014
 * Время: 17:45
 * 
 * Для изменения этого шаблона используйте Сервис | Настройка | Кодирование | Правка стандартных заголовков.
 */
using System;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;

namespace hwspeed
{
    /// <summary>
    /// Description of SelModem.
    /// </summary>
    public partial class fmSelModem : Form
    {
        public fmSelModem()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            Icon = Configuration.MainIcon;
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        
        private string _selmodem_port = null;
            
        public string SelectedModem {
            get {
                return _selmodem_port;
            }
        }
        
        void FmSelModemLoad(object sender, EventArgs e)
        {
        }
        
        void BtOkClick(object sender, EventArgs e)
        {
            if (lvModems.SelectedItems.Count > 0)
            {
                _selmodem_port = lvModems.SelectedItems[0].SubItems[1].Text;
                DialogResult = DialogResult.OK;
            }
        }
        
        void FmSelModemShown(object sender, EventArgs e)
        {
            lvModems.Items.Clear();
            var ssports = SerialPort.GetPortNames();
            progressBar1.Maximum = ssports.Length;
            progressBar1.Value = 0;
            progressBar1.Visible = true;
            for ( int i = ssports.Length-1; i >= 0; --i )
            {
                try
                {
                    Modem md = new Modem(ssports[i]);
                    if (md.IsHuaweiE3131())
                    {
                        var item = new ListViewItem("HUAWEI E3131");
                        item.SubItems.Add(md.ModemPort);
                        lvModems.Items.Add(item);
                    }
                    md.Reset();
                } catch (Exception){}
                progressBar1.Value++;
            }
            progressBar1.Visible = false;
        }
        
        void LvModemsItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            btOk.Enabled = lvModems.SelectedItems.Count > 0;
        }
        
    }
}
